package com.yuandengta.yomall.product.service.impl;

import com.yuandengta.yomall.product.dao.BrandDao;
import com.yuandengta.yomall.product.dao.CategoryDao;
import com.yuandengta.yomall.product.entity.BrandEntity;
import com.yuandengta.yomall.product.entity.CategoryEntity;
import com.yuandengta.yomall.product.service.BrandService;
import com.yuandengta.yomall.product.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuandengta.common.utils.PageUtils;
import com.yuandengta.common.utils.Query;

import com.yuandengta.yomall.product.dao.CategoryBrandRelationDao;
import com.yuandengta.yomall.product.entity.CategoryBrandRelationEntity;
import com.yuandengta.yomall.product.service.CategoryBrandRelationService;


@Service("categoryBrandRelationService")
public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationDao, CategoryBrandRelationEntity> implements CategoryBrandRelationService {

    @Autowired
    private BrandDao brandDao;

    @Autowired
    private CategoryDao categoryDao;

    @Autowired
    private CategoryBrandRelationDao categoryBrandRelationDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryBrandRelationEntity> page = this.page(
                new Query<CategoryBrandRelationEntity>().getPage(params),
                new QueryWrapper<CategoryBrandRelationEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 获取品牌关联的所有分类
     * @param brandId
     * @return
     */
    @Override
    public List<CategoryBrandRelationEntity> list(Long brandId) {
        List<CategoryBrandRelationEntity> brandRelationEntities =
                this.list(new QueryWrapper<CategoryBrandRelationEntity>().eq("brand_id", brandId));
        return brandRelationEntities;
    }

    /**
     * 新增品牌分类
     * @param categoryBrandRelation
     */
    @Override
    public void saveDetail(CategoryBrandRelationEntity categoryBrandRelation) {
        // 两次查询
        // 获取品牌名
        BrandEntity brandEntity = brandDao.selectById(categoryBrandRelation.getBrandId());
        // 获取分类名
        CategoryEntity categoryEntity = categoryDao.selectById(categoryBrandRelation.getCatelogId());
        // 设置
        categoryBrandRelation.setBrandName(brandEntity.getName());
        categoryBrandRelation.setCatelogName(categoryEntity.getName());

        this.save(categoryBrandRelation);
    }

    /**
     * 获取分类关联的品牌
     * @param catId 分类ID
     * @return 当前分类下的品牌
     */
    @Override
    public List<BrandEntity> getBrandsByCatId(Long catId) {
        List<CategoryBrandRelationEntity> relationEntities = categoryBrandRelationDao.selectList(
                new QueryWrapper<CategoryBrandRelationEntity>().eq("catelog_id", catId));

        List<BrandEntity> brandEntities = relationEntities.stream().map(entity -> {
            Long brandId = entity.getBrandId();
            BrandEntity brandEntity = brandDao.selectById(brandId);
            return brandEntity;
        }).collect(Collectors.toList());

        return brandEntities;
    }
}